package com.thermofisher.dsc.amanda.customer.service.impl;

import com.thermofisher.dsc.amanda.auth.service.UserService;
import com.thermofisher.dsc.amanda.common.Query;
import com.thermofisher.dsc.amanda.common.crud.service.AbsBaseService;
import com.thermofisher.dsc.amanda.customer.mapper.ITSCARMapper;
import com.thermofisher.dsc.amanda.customer.service.ITSCARService;
import com.thermofisher.dsc.amanda.model.dto.customer.ITSCARDto;
import com.thermofisher.dsc.amanda.model.po.customer.ITSCAR;
import com.thermofisher.dsc.amanda.model.response.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by wenjie.yang on 1/3/2019.
 */
@Service
public class ITSCARServiceImpl extends
        AbsBaseService<ITSCARDto, ITSCAR, ITSCARMapper> implements ITSCARService {

    /**
     * 默认构造函数
     */
    public ITSCARServiceImpl(ITSCARMapper mapper) {
        super(mapper);
    }

    @Autowired
    private UserService userService;

  /*@Override
  public List<CustomerDto> getHierarchyCompany(Query query) {
    Long currentId = SessionUtil.getUserId();
    List<Long> userIds = userService.getCurrentHierarchyUserIds(currentId, null);
    return null;
  }*/


    @Override
    public PageResponse getList(Query query) {
    /*if (query.isOrderByClauseDefault()) {
      query.setOrderByClause("CONVERT(name USING gbk)");
    }*/
        PageResponse response = super.getList(query);
        BigDecimal totalAmount = mapper.getTotalAmount(query.get("bill2NO").toString());
        if (totalAmount == null){
            response.setTotalAmount(new BigDecimal(0));
        }
        response.setTotalAmount(totalAmount);
        return response;
    }

 /* @Override
  public Map<String, Object> getEmployeeData() {
    Double employeeAvg = mapper.getEmployeeAvg();
    List<Integer> employeeList = mapper.getEmployeeNumberAsc();
    double median = 0;
    if (employeeList.size() % 2 == 0) {
      median = (double) (employeeList.get(employeeList.size() / 2 - 1) + employeeList
          .get(employeeList.size() / 2)) / 2;
    } else {
      median = (double) employeeList.get(employeeList.size() - 1 / 2 + 1);
    }
    Map<String, Object> data = new HashMap<>(2);
    data.put("avg", Math.round(employeeAvg));
    data.put("median", median);
    return data;
  }


  @Override
  public Map<String, Object> getTopSales(Long companyId) {
    List<User> userList = mapper.getTopSales(companyId);
    Map<String, List<UserDto>> collect = userList.stream().map(u -> Util.convert(u, UserDto.class))
        .collect(Collectors.groupingBy(UserDto::getDepartment));
    Map<String, Object> topSales = new LinkedHashMap<>();
    DEPARTMENT.stream().forEach((dep) -> {
      List<UserDto> userDtoList = collect.get(dep);
      topSales.put(dep, userDtoList == null||userDtoList.isEmpty() ? null : userDtoList.get(0));
    });
    return topSales;
  }

  @Override
  public CompanyDto getById(Long id) {
    CompanyDto companyDto = super.getById(id);
    setFinanceAndProductMsg(companyDto);
    return companyDto;
  }

  @Override
  public ChartsBean getRevenueData(Long companyId, String startYear) {
    ChartsBean chartsBean = new ChartsBean();
    List<Revenue> revenues = mapper.getRevenueDataByCompanyId(companyId, startYear);
    Map<String, List<Revenue>> mapData = revenues.stream()
        .collect(Collectors.groupingBy(r -> TimeUtils.formateDate(r.getRevenueYear(), "yyyy")));
    Integer yearInt = Integer.valueOf(startYear);
    chartsBean.setXAxis(DEPARTMENT);
    IntStream.range(yearInt, yearInt + 4).forEach(i -> {
      Map<String, Object> seriesCell = chartsBean.initKeyMap();
      seriesCell.put(ChartsBean.SERIES_CELL_HEADER, i + "");
      if (mapData.get(i + "") != null) {
        mapData.get(i + "").forEach(r -> {
          seriesCell.put(r.getDepartment(), Math.round(r.getAmount()));
        });
      }
      chartsBean.getSeries().add(seriesCell);
    });
    return chartsBean;
  }


  @Override
  public List getAreaList(String province) {
    List<String> areas  = mapper.getArea(province).stream().filter(s->StringUtils.isNotBlank(s)).collect(
        Collectors.toList());
    return areas;
  }


  @Override
  public Map<String, List> getFilterConditions() {
    Map<String, List> conditions = new HashMap<>(3);
    List<String> businessType =  mapper.getCompanyTypes().stream().filter(s->StringUtils.isNotBlank(s)
    ).collect(Collectors.toList());
    List<String> researchProdStatus  = mapper.getCompanyResearchProdStatus();
    List<String> foundedYear =   mapper.getCompanyfoundedYear();
    conditions.put("businessType_OR_subBusinessType",businessType);
    conditions.put("researchProdStatus",researchProdStatus);
    conditions.put("foundedYear",foundedYear);
    return conditions;
  }*/
}
